//index.js
//获取应用实例
const app = getApp()
const {requestDataparams} = require('../../../utils/request.js');
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
Page({
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    currentUserInfo: {},
    currentUseInfoPage: {},
    checkValues: [],
    currentSwichIds: '',
    status: false,
    // 权限
    visitorPermissions: [],
    // 闸机
    gates: []
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    this.setData({
      currentUserInfo: JSON.parse(options.currentUseInfo)
    })
    this.initPage()
  },
  // checkbox 的变化
  checkBoxChange(e){
     this.setData({
        checkValues: e.detail.value
     })
     this.setData({
       status: true
     })
    //  let changeCheckValue = e.detail.value
     
    //  let gates = this.data.gates
    //  gates.forEach(ele=>{
    //     if(changeCheckValue.includes(JSON.stringify(ele.id)) && !ele.checked){
    //       // if(ele.)
    //       ele.checked = true
    //     }
    //  })
    //  setTimeout(() => {
    //   this.setData({
    //     gates
    //   })
    //  }, 500);

  },
  // swich切换
  swichChange(e){
    // this.currentSwichIds
    // if(e.detail.value){
      let currentStatus = e.detail.value
      let id = e.currentTarget.dataset.id
      let  visitorPermissions=this.data.currentUseInfoPage.visitorPermissions
      visitorPermissions.forEach(ele=>{
            if(ele.id==id){
              ele.checked = currentStatus
            }
      })
      this.setData({
        visitorPermissions
      })
    // } else 
    
  },
  // 页面初始化
  initPage(){
    let id = this.data.currentUserInfo.id
    
    requestDataparams('/api/visitors/enterpriseStaffs/'+id,{},'GET').then(response=>{
      if(response.data.code==0){
        
        let currentCheckValue = response.data.data.gates
        let arr = []
        currentCheckValue.forEach(ele=>{
          if(ele.checked){
            arr.push(ele.id)
          }
        })
        this.setData({
          currentUseInfoPage: response.data.data,
          checkValues: arr,
          visitorPermissions: response.data.data.visitorPermissions,
          gates: response.data.data.gates,
        })
      }
    })
  },
  // 保存数据
  saveData(){
    let id = this.data.currentUserInfo.id;
    let sysUserId = this.data.currentUserInfo.sysUserId;

    let visitorPermissionIds=[]
    let gateIds=[]
    if( this.data.status){
       gateIds = this.data.checkValues
    } else {
      this.data.gates.forEach(ele=>{
        if(ele.checked){
          gateIds.push(ele.id)
        }
      })
    }
    
    this.data.visitorPermissions.forEach(ele=>{
      if(ele.checked){
        visitorPermissionIds.push(ele.id)
      }
    })
    requestDataparams('/api/visitors/enterpriseStaffs', {visitorPermissionIds:visitorPermissionIds.join(','),gateIds:gateIds.join(','),id,sysUserId}, 'POST').then(response=>{
           if(response.data.code == 0){
             wx.showToast({
               title: '设置成功',
               icon: 'none'
             })
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2]; //上一个页面
            wx.navigateBack({
              delta: 1
            })
           }
    })
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
